import pandas as pd

df = pd.read_csv('static/data/scenic_spots_info_clean_with_location.csv')
df_liner = pd.read_csv('static/data/liner_Regression.csv')
df_word_cloud = pd.read_csv('static/data/word_cloud.csv')


def map_data():
    df_map = df[df['location'] != ',']
    data = [
        {
            'name': i[0],
            'value': i[1].split(',') + [i[2]]
        } for i in df_map[['name', 'location', 'score']].values
    ]
    return {
        'data': data
    }


def comment_level(x):
    if x > 4000:
        return 5
    if 3000 < x <= 4000:
        return 4
    if 2000 < x <= 3000:
        return 3
    if 1000 < x <= 2000:
        return 2
    if x <= 1000:
        return 1


def radar_data():
    df['comment_level'] = df['number_of_comments'].apply(comment_level)
    name = [
        {
            'name': '热度',
            'max': 10
        },
        {
            'name': '评分',
            'max': 5
        },
        {
            'name': '星级',
            'max': 5
        },
        {
            'name': '评论数',
            'max': 5
        },
    ]
    value = [
        {
            'value': list(i[1:]),
            'name': i[0]
        } for i in df[['name', 'heat', 'score', 'star_level', 'comment_level']].values]
    data = {
        'name': name,
        'value': value
    }
    return data


def heat_top10():
    df_bar = df.sort_values(by='heat', ascending=False).head(10)
    name = df_bar.name.values.tolist()
    value = df_bar.heat.values.tolist()
    data = {
        'name': name,
        'value': value
    }
    return data


def scatter_data():
    df_scatter = df[['heat','score']]
    scatter_list = df_scatter.values.tolist()
    liner_list = df_liner.values.tolist()
    return {
        'scatter': scatter_list,
        'liner': liner_list
    }


def comment_word_frequency_top50():
    data = [{'name': i[0], 'value': i[1]} for i in df_word_cloud.head(50).values.tolist()]
    return {
        'data': data
    }
